/*!
 * 组件类模块（MVC）
 * @author Sherlock
 * 偏业务型工具类， 涵盖了html 和较多的文案
 */
require( './style.less' );

define( [
  'jquery',
  'message',
  'constants'
], function( $, ShowTitleMessage, cst ) {
  return {
    /**
     * 确认是否删除的弹出tooltips<br/>
     * @param obj 确认框的位置目标对象
     * @param url 异步请求的地址
     * @param fun
     * @param msg 提示消息
     * @param param 参数
     * @param type 请求方法 post/get
     * @param custom 此参数赋值后，将覆盖所有默认的确认按钮操作
     * bug修改 无ajax请求时 过慢问题
     */
    deleteConfirmShowBottom: function( option ) {
      var defaultOption = {
        text: '确认要删除吗？',
        skewX: 0
      };
      option = $.extend( defaultOption, option );
      $( ".delete_confirm" ).remove();
      var obj = $( option.obj );
      var top = obj.offset().top;
      var left = obj.offset().left;

      if ( !obj.attr( 'delete-event' ) ) obj.attr( 'delete-event', '' );
      var height = obj.outerHeight();
      var width = obj.outerWidth();
      // 减去确认框一半的宽度
      left = left - 106 + width / 2;
      // 如果有偏移的话
      left += option.skewX;

      var $deleteDialog = $( '<div class="delete_confirm">' +
        '<div class="arrow" style="margin-left: ' + ( -option.skewX - 11 ) + 'px"></div>' +
        '<p class="del_text">' + option.text + '</p>' +
        '<button type="button" class="confirm_sure bigwe_default_btn">确认</button>' +
        '<button type="button" class="confirm_cancel">取消</button>' +
        '</div>' ).appendTo( 'body' ).css( option.style || {} );

      var dialogHeight = $deleteDialog[ 0 ].clientHeight;
      var arrowHeight = 13;
      // 确认框完整的出现在屏幕内
      if ( top + height + arrowHeight + dialogHeight < ( $( document ).scrollTop() + $( window ).height() ) ) {
        top += height + arrowHeight;
        $deleteDialog.addClass( 'bottom' );
      } else {
        top -= dialogHeight + arrowHeight;
        $deleteDialog.addClass( 'top' );
      }
      // 加上确认框尖角的高度
      $deleteDialog.css( {
        top: top,
        left: left
      } );

      // 点击确认按钮
      $( ".confirm_sure", $deleteDialog ).on( "click", function( e ) {
        e.stopPropagation();
        // 自定义确定按钮后，其他确认操作均无效
        if ( $.isFunction( option.custom ) ) {
          option.custom.call( option.obj );
          return;
        }
        $( ".delete_confirm" ).remove();
        // 移除点击其他区域隐藏分类选择的事件
        $( document ).unbind( "click.delete_confirm" );
        if ( option.url ) {
          // 删除
          var param = option.param ? option.param : {};
          // 显示提示消息
          if ( option.msg != '' && typeof( option.msg ) != 'underfined' ) {
            ShowTitleMessage.show( option.msg );
          }
          $.ajax( {
            url: option.url,
            type: option.type || 'get',
            data: param,
            success: function() {
              if ( $.isFunction( option.fun ) ) {
                option.fun.apply( obj, arguments );
              }
            }
          } );
        } else {
          if ( option.fun ) {
            option.fun.call( obj );
          }
        }
      } );
      $( ".confirm_cancel", $deleteDialog ).on( "click", function( e ) {
        e.stopPropagation();
        $( this ).parent().css( {
          'animation': 'fadeOutToBig ease .3s'
        } );
        setTimeout( function() {
          $( ".delete_confirm" ).remove();
          // 移除点击其他区域隐藏分类选择的事件
          $( document ).unbind( "click.delete_confirm" );
        }, .2e3 );
        if ( option.cancel ) {
          option.cancel.call( obj );
        }
      } );
      /**
       * 点击其他地方隐藏
       * 请务必声明删除的按钮或标签有delete-event属性
       */
      $( document ).bind( "click.delete_confirm", function( e ) {
        var target = $( e.target );
        if ( typeof( target.attr( "delete-event" ) ) != 'undefined' /* sherlock 待测试 && target.attr("delete-event") != ""*/ ) {
          return;
        }
        if ( target.closest( ".delete_confirm" ).length == 0 ) {
          $( ".delete_confirm" ).remove();
          $( document ).unbind( "click.delete_confirm" );
        }
        if ( option.cancel ) {
          option.cancel.call( obj );
        }
      } );
    },
    /**
     * 确认弹出框
     * @param  {[type]}   ''       [description]
     * @param  {Function} fn       [description]
     * @param  {[type]}   cancelfn [description]
     * @return {[type]}            [description]
     */
    confirm: function( text, fn, cancelfn ) {


      if ( typeof text === 'function' ) {
        cancelfn = fn;
        fn = text;
        text = '导入图文后会覆盖编辑器中已有内容，确定要覆盖吗？';
      }

      var html = [ '<div class="modal fade override-confirm" data-backdrop="static" id="override-confirm" tabindex="-1" role="dialog">',
        '<div class="modal-dialog">',
        '<div class="modal-content clearfix">',
        '<div class="modal-header">',
        '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
        '<h4 class="modal-title">温馨提示</h4>',
        '</div>',
        '<div class="modal-body">',
        '<span><span class="iconfont icon-warning warning-logo"></span><span class="text gray">' + text + '</span></span>',
        '</div>',
        '<div class="modal-footer">',
        '<button type="button" class="btn btn-success js_sure">确定</button>',
        '<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>',
        '</div>',
        '</div>',
        '</div>',
        '</div>'
      ].join( '' );
      var $overrideConfirm = null;

      if ( !$( '#override-confirm' ).length ) {
        $overrideConfirm = $( html ).appendTo( document.body ).modal( 'show' );
      } else {
        $overrideConfirm = $( '#override-confirm' ).modal( 'show' );
        $( 'span.text', $overrideConfirm ).text( text );
      }

      $overrideConfirm.one( 'hide.bs.modal', function() {
        $( '.js_sure', this ).off( 'click.override' );
        if ( !this.clicked ) {
          cancelfn && cancelfn();
        }
        this.clicked = false;
      } );

      $( '.js_sure', $overrideConfirm ).on( 'click', function() {
        $.isFunction( fn ) && fn();
        $overrideConfirm[ 0 ].clicked = true;
        $overrideConfirm.modal( 'hide' );
      } );
    }
  }
} );
